package com.neu.view.impl;

import com.neu.dao.AdminDao;
import com.neu.dao.MemberDao;
import com.neu.dao.impl.AdminDaoImpl;
import com.neu.dao.impl.MemberDaoImpl;
import com.neu.view.UserView;
import static com.neu.util.Read.*;

import java.util.List;
import java.util.Map;

public class AdminView extends UserView {
    private AdminDao adminDao = new AdminDaoImpl();
    private MemberDao memberDao = new MemberDaoImpl();
    @Override
    public Map<String, Object> login() {
        Map<String,Object> map=null;
        System.out.print("请输入管理员账号：");
        String name = getIn().next();
        System.out.print("请输入管理员密码：");
        String pwd = getIn().next();
        map = adminDao.login(name,pwd);
        return map;
    }

    @Override
    public void menu() {
        A:while (true) {
            System.out.println("管理员权限如下");
            System.out.println("1.添加成员");
            System.out.println("2.删除成员");
            System.out.println("3.显示所有成员");
            System.out.println("4.注销");
            System.out.print("请选择：");
            int n = getIn().nextInt();
            switch (n){
                case 1:
                    insertMember();
                    break;
                case 2:
                    deleteMember();
                    break;
                case 3:
                    searchMemberAll();
                    break;
                case 4:
                    break A;
                default:
                    System.out.println("请正确选择!");
            }
        }
    }

    private void searchMemberAll() {
        List<Map<String,Object>> list = memberDao.findAll();
        System.out.println("成员编号\t成员名称");
        for (int j = 0;j<list.size();j++){
            Map<String,Object> map = list.get(j);
            Object id =map.get("member_id");
            Object name =map.get("member_name");
            System.out.println("\t"+id+"\t"+name);
        }
    }

    private void deleteMember() {
        int row=0;
        System.out.print("请输入删除的成员名称：");
        String name =getIn().next();
        System.out.println("是否确认删除？");
        System.out.println("1.确认删除");
        System.out.println("2.返回");
        System.out.print("请选择：");
        int x=getIn().nextInt();
        if (x==1) {
            row = memberDao.deleteMember(name);
        }
        if (row>0){
            System.out.println("删除成功!");
            System.out.println();
        }
    }

    private void insertMember() {
        int row =0;
        System.out.println("请输入家庭成员名称：");
        String name = getIn().next();
        row = memberDao.insertMember(name);
        if (row>0){
            System.out.println("添加成功，默认密码888888");
        }
    }
}
